package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.common.utils.PageUtils;
import com.yex.dao.InvestmentDao;
import com.yex.entity.InvestmentEntity;
import com.yex.service.DynamicLogService;
import com.yex.service.InvestmentLogService;
import com.yex.service.InvestmentService;
import com.yex.service.LocaleMessageSourceService;
import com.yex.utils.JsonResultVo;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("investmentService")
public class InvestmentServiceImpl extends ServiceImpl<InvestmentDao, InvestmentEntity> implements InvestmentService {
    @Autowired
    private InvestmentLogService investmentLogService;
    @Autowired
    private LocaleMessageSourceService message;
    @Autowired
    private DynamicLogService dynamicLogService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<InvestmentEntity> page = this.selectPage(
                new Query<InvestmentEntity>(params).getPage(),
                new EntityWrapper<InvestmentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public InvestmentEntity selectByOne() {
        return baseMapper.selectByOne();
    }

    @Override
    public InvestmentEntity selectEndTimeByOne() {
        return baseMapper.selectEndTimeByOne();
    }

    @Override
    public InvestmentEntity selectEndTimeByDynamic() {
        return baseMapper.selectEndTimeByDynamic();
    }

    @Override
    @Transactional
    public Integer updateDynamicStatus(Integer investmentId) {
        return baseMapper.updateDynamicStatus(investmentId);
    }

    @Override
    @Transactional
    public Integer updateEndTimeByDynamicStatus(Integer investmentId) {
        return baseMapper.updateEndTimeByDynamicStatus(investmentId);
    }

    @Override
    public JsonResultVo selectUserInvestment(Integer userId) {
        BigDecimal introUserTotal = investmentLogService.selectIntroUserTotal(userId);
//        BigDecimal introDownTotal = investmentLogService.selectIntroDownTotal(userId);
        BigDecimal introTotal = investmentLogService.selectIntroTotal(userId);
        BigDecimal leaderTotal =  dynamicLogService.selectLeaderTotal(userId);
        List<Map<String, Object>> mapList = new ArrayList<>(10);
        Map<String, Object> map = new HashMap<>(3);
        map.put("name","直推奖");
        map.put("val", introUserTotal);
        mapList.add(map);

        map = new HashMap<>(3);
        map.put("name","管理奖");
        map.put("val", leaderTotal);
        mapList.add(map);

        map = new HashMap<>(3);
        map.put("name","团队奖");
        map.put("val", introTotal);
        mapList.add(map);

        return new JsonResultVo(message.getMessage("success"), mapList, 0);
    }


}
